//
//  ClassifyButtonView.m
//  NewMusic
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "ClassifyButtonView.h"
@interface ClassifyButtonView ()
@property(nonatomic, retain) UILabel *lineLabel;

@end


@implementation ClassifyButtonView
- (void)dealloc {
     [_backView release];
    [_lineLabel release];
    [_titleArray release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = [NSMutableArray array];
        self.backgroundColor = [UIColor clearColor];
        //背景图片
        self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _backView.backgroundColor = [UIColor blackColor];
        _backView.alpha = 0.7;
        [self addSubview:_backView];
        [_backView release];

    }
    return self;
}
#pragma mark 重写titleArray的setter方法
- (void)setTitleArray:(NSMutableArray *)titleArray {
    if (_titleArray != titleArray) {
        [_titleArray release];
        _titleArray = [titleArray retain];
    }
    //创建视图
    [self createSubView];
}
#pragma mark 重写showLength的setter方法
- (void)setShowLength:(BOOL)showLength {
    if (_showLength != showLength) {
        _showLength = showLength;
    }
    [self createLineView];
}
#pragma mark 创建视图
-(void)createSubView {
           for (int i = 0; i < _titleArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0 + self.frame.size.width / _titleArray.count * i , 10, self.frame.size.width / _titleArray.count, self.frame.size.height - 15);
        [button setTitle:_titleArray[i] forState:UIControlStateNormal];
        button.tag = 2500 + i;
//        button.backgroundColor = [UIColor cyanColor];
        if (i == 0) {
            [button setTitleColor:[UIColor colorWithRed:56 / 255.0 green:192 / 255.0 blue:107 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        } else {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    }
- (void)createLineView {
    self.lineLabel = [[UILabel alloc]init];
    if (_showLength == YES) {
        _lineLabel.frame = CGRectMake((self.frame.size.width - 250) / 3, self.frame.size.height - 2, 100, 2);
    }else if (_showLength == NO){
        if (_titleArray.count != 0) {
            _lineLabel.frame = CGRectMake(0, self.frame.size.height - 2, self.frame.size.width / _titleArray.count, 2);

        }
    }
    _lineLabel.backgroundColor = [UIColor colorWithRed:56 / 255.0 green:192 / 255.0 blue:107 / 255.0 alpha:1.0];
    [self addSubview:_lineLabel];
    [_lineLabel release];

}
#pragma mark button的点击事件
- (void)buttonAction:(UIButton *)button {
    for (UIButton *btn in self.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            if (btn.tag == button.tag) {
                [btn setTitleColor:[UIColor colorWithRed:56 / 255.0 green:192 / 255.0 blue:107 / 255.0 alpha:1.0] forState:UIControlStateNormal];
            }else {
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }

        }
    }
    [UIView animateWithDuration:0.2 animations:^{
        if (self.showLength == YES) {
            self.lineLabel.frame = CGRectMake((self.frame.size.width - 250) / 3 + (self.frame.size.width / 2 ) * (button.tag - 2500), self.lineLabel.frame.origin.y, self.lineLabel.frame.size.width, 2);
        }else {
        self.lineLabel.frame = CGRectMake(button.frame.origin.x, self.lineLabel.frame.origin.y, self.lineLabel.frame.size.width, 2);
        }
        
    }];
    [self.delegate buttonClickAction:button];
}
#pragma mark 重写buttonTag的setter方法
- (void)setButtonTag:(NSInteger)buttonTag {
    if (_buttonTag != buttonTag) {
        _buttonTag = buttonTag;
    }
    UIButton *button = (UIButton *)[self viewWithTag:buttonTag];
    [self buttonAction:button];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
